package com.xiaoxu.utils;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class LogParser {

    String a = "John Doe\u0001100000.0\u0001Mary Smith\u0002Todd Jones\u0001Federal Taxes\u0003.2\u0002State Taxes\u0003.05\u0002Insurance\u0003.1\u00011 Michigan Ave.\u0002Chicago\u0002IL\u000260600\n" +
            "Mary Smith\u000180000.0\u0001Bill King\u0001Federal Taxes\u0003.2\u0002State ";

    private Logger logger = LoggerFactory.getLogger(LogParser.class);



    public Map<String, String> parse2(String log)  {

        Map<String, String> logInfo = new HashMap<String,String>();
        IPParser ipParse = IPParser.getInstance();
        if(StringUtils.isNotBlank(log)) {
            String[] splits = log.split("\t");

            String ip = splits[0];
            String url = splits[1];
            String sessionId = splits[2];
            String time = splits[3];
            String country = splits[4];
            String province = splits[5];
            String city = splits[6];

            logInfo.put("ip",ip);
            logInfo.put("url",url);
            logInfo.put("sessionId",sessionId);
            logInfo.put("time",time);
            logInfo.put("country",country);
            logInfo.put("province",province);
            logInfo.put("city",city);

        } else{
            logger.error("日志记录的格式不正确：" + log);
        }

        return logInfo;
    }

    /**
     * 20946835322
     * http://www.yihaodian.com/1/?tracker_u=2225501&type=3
     * http://www.baidu.com/s?wd=1%E5%8F%B7%E5%BA%97&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_sug3=5&rsv_sug=0&rsv_sug1=4&rsv_sug4=313&inputT=4235
     * 1号店
     * 1
     * SKAPHD3JZYH9EE9ACB1NGA9VDQHNJMX1NY9T
     * PPG4SWG71358HGRJGQHQQBXY9GF96CVU
     * 2225501\N124.79.172.232
     * msessionid:YR9H5YU7RZ8Y94EBJNZ2P5W8DT37Q9JH,unionKey:2225501
     * 2013-07-21 09:30:01
     * \Nhttp://www.baidu.com/s?wd=1%E5%8F%B7%E5%BA%97&rsv_bp=0&ch=&tn=baidu&bar=&rsv_spt=3&ie=utf-8&rsv_sug3=5&rsv_sug=0&rsv_sug1=4&rsv_sug4=313&inputT=4235
     * 1\Nnull-10
     * Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; MATP; Media Center PC 6.0; .NET4.0C; InfoPath.2; .NET4.0E)
     * Win32
     * 上海市
     * 12013-07-21 09:30:01上海市
     * 66\N\N\N\N2013-07-21
     * @param log
     * @return
     */
//fdgf,gfdg,dfgfd,dfgfd,g

    public Map<String, String> parse(String log)  {

        Map<String, String> logInfo = new HashMap<String,String>();
        IPParser ipParse = IPParser.getInstance();
        if(StringUtils.isNotBlank(log)) {
            //^ 编码为 \001 "\u0001"
            String[] splits = log.split("\u0001");

            String ip = splits[13];
            String url = splits[1];
            String sessionId = splits[10];
            String time = splits[17];

            logInfo.put("ip",ip);
            logInfo.put("url",url);
            logInfo.put("sessionId",sessionId);
            logInfo.put("time",time);


            IPParser.RegionInfo regionInfo = ipParse.analyseIp(ip);

            logInfo.put("country",regionInfo.getCountry());
            logInfo.put("province",regionInfo.getProvince());
            logInfo.put("city",regionInfo.getCity());
            
        } else{
            logger.error("日志记录的格式不正确：" + log);
        }

        return logInfo;
    }

}